﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SearchInterface
{
    class Controller
    {
        private SearchListener listener;
        private helper.ThriftServerThread listenerthread;
        private Callback display;
        private hydrap2p.search.Searcher.Iface searcher;
        public Controller(Callback display)
        {
            listener = new SearchListener(this);
            this.display = display;


            try
            {
                listenerthread = new helper.ThriftServerThread(new hydrap2p.search.SearchListener.Processor(listener));
                listenerthread.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                shutdown();
                throw new Exception("Failed to connect to the Manager: "+e.Message);
            }

            try
            {
                searcher = helper.ServiceConnector.GetSearcher();
                searcher.subscribeListener(listenerthread.GetLocation());
            }
            catch (Exception e)
            {
                shutdown();
                throw new Exception("Failed to connect to the Search Manager: "+e.Message);
            }
        }

        public void shutdown()
        {
            if (listenerthread!=null)
                listenerthread.Shutdown();
        }

        public void startSearch(string keywords)
        {
            hydrap2p.search.SearchQuery query = new hydrap2p.search.SearchQuery()
            {
                Keywords = keywords,
                Name = keywords
            };
            searcher.startSearch(query);
        }

        public List<String> getSearchers()
        {
            return searcher.listSearchProviders();
        }

        public void download(String name, int searchid, int resultid)
        {
            searcher.download(helper.Utilities.GetUserConfigurationPath(), name, searchid, resultid);
        }

        public void newSearch(int searchid, hydrap2p.search.SearchQuery query, List<String> providers)
        {
            display.addSearch(searchid, query, providers);
            helper.ServiceConnector.GetSearcher().subscribeSearchListener(listenerthread.GetLocation(), searchid);
        }
        public void searchDeleted(int searchid)
        {
            display.delSearch(searchid);
        }
        public void deleteSearch(int searchid)
        {
            searcher.deleteSearch(searchid);
        }
        public void addSearchResult(int searchid, hydrap2p.search.SearchResult result)
        {
            display.addSearchResult(searchid, result);
        }
        public void searchFinished(int searchid)
        {
            display.searchFinished(searchid);
        }
        public void searchProviderFinished(int searchid, String providername)
        {
            display.searchProviderFinished(searchid, providername);
        }
    }
}
